package practice_05;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class SumArrayList {
    public static void main(String[] args) {
        printArrayList();
    }

    //    键盘录入整数存到集合，直到总和大于200.
    public static void sumArray() {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> arrayList = new ArrayList<>();
        int sum = 0;
        while (true) {
            System.out.print("请输入一个整数:");
            int x = sc.nextInt();
            arrayList.add(x);
            if (sumList(arrayList) > 200) {
                System.out.println("已经大于200:" + sumList(arrayList));
                break;
            }
        }

    }

    private static int sumList(ArrayList<Integer> arrayList) {
        int sum = 0;
        for (Integer integer : arrayList) {
            sum += integer;
        }
        return sum;
    }

    //遍历ArrayList
    public static void printArrayList() {
        ArrayList<String> list = new ArrayList<>();
        list.add("洛");
        list.add("天");
        list.add("依");
        for (String s : list) {
            System.out.print(s + " ");
        }

        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }

        list.forEach(s -> System.out.print(s + " "));
    }
}
